package org.brisling.duties.queryService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletResponse;

import org.brisling.base.domain.ExportExcelDomain;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.ExportUtil;
import org.brisling.common.util.JsonUtil;
import org.brisling.common.util.StringUtil;
import org.brisling.duties.domain.HighValue;
import org.brisling.ethic.domain.EthicEvaluate;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@RequestMapping("/highValue/query")
public class QueryHighvalueService extends AbstractBaseQueryService<HighValue>{
	
	
	@Autowired
	protected QueryDictionaryService dicService;
	
	private String title = "高值耗材统计表";
	
	public QueryHighvalueService(){
		this.moduleName = ConstSysParam.MODULE_DUTIES_HIGHVALUE;
		this.subSystem = ConstSysParam.SUBSYSTEM_DUTIES;
		this.tableName = ConstSysParam.VIEW_PREFIX +this.getTableName(HighValue.class);
		this.clazz = HighValue.class;		
		
		typeMap = this.getTypeMap(clazz);
		
		
		
		orderMap.put("sqsj","desc");	
		orderMap.put("sxbh","desc");	
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<HighValue> getListDomain(@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
	}
	
	@RequestMapping("/getList.action")
	public List<HighValue> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	/**
	 * 根据业务id查找业务信息
	 * @param id	业务id
	 * @return 业务信息记录 
	 */
	@RequestMapping("/getOne.action")
	public HighValue getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据业务唯一编码查找业务信息
	 * @param identify	业务唯一编码
	 * @return 业务信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public HighValue getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	
	
	/**
	 * 导出excel记录
	 * @param body
	 * @param response
	 */
	@RequestMapping("/exportExcel.action")
	public void exportExcel(@RequestBody(required=false) String body,HttpServletResponse response){
				
		
		
		
		Map<String,Object> conditionMap = this.getBodyParam(body);
		Map<String,String> param_map = (Map<String, String>) conditionMap.get(ConstSysParam.CONDITION_MAP_KEY);
		
		//判断导出全部还是导出当前页
		if(param_map!=null && param_map.containsKey("exportAll")){
			//导出全部
			Object obj = param_map.get("exportAll");
			if( (obj!=null) && (obj.toString().compareTo("1")==0) ){
//				this.exportExcelAll(body, response);
				this.exportExcelAll(body, response);
			}else{
				//导出当前页
				ListDomain<HighValue> dls = this.getListDomain(body);
				
				if(dls!=null && dls.getTotal()>0){ 
					List<HighValue> lists=dls.getRows();
				
					this.exportToFile(lists, response);
				}
			}
		}else{
			
			//导出当前页
			ListDomain<HighValue> dls = this.getListDomain(body);
			
			if(dls!=null && dls.getTotal()>0){ 
				List<HighValue> lists=dls.getRows();
			
				this.exportToFile(lists, response);
			}
		}
		
	}
	
	private void exportExcelAll(@RequestBody(required=false) String body,HttpServletResponse response){
		
		Map<String,Object> conditionMap = this.getBodyParam(body);
		Map<String,String> param_map = (Map<String, String>) conditionMap.get(ConstSysParam.CONDITION_MAP_KEY);
		param_map =param_map == null?new HashMap<String,String>():param_map;
		
		List<HighValue> lists=this.getListByConditionAndOrder(param_map,typeMap,orderMap,true,false);
		
		this.exportToFile(lists, response);
	}
	
	private void exportToFile(List<HighValue> lists ,HttpServletResponse response){
		
		String configId = "highvalue_record_export";
		String configStr = dicService.getSqlByBizcode(configId);
		
		ObjectMapper mapper = JsonUtil.getMapper();
		JavaType _javaType = mapper.getTypeFactory().constructCollectionType(List.class, ExportExcelDomain.class);

		List<ExportExcelDomain> list = null;
		try {
			list =  mapper.readValue(configStr, _javaType);
		}catch(Exception e){
			e.printStackTrace();
		}
		ExportUtil<HighValue> eu = new ExportUtil<HighValue>(title,list,lists);
		try {
			eu.exportByDomain(response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
		
	
}
